Is there a method to delete characters?
No — there are only methods that append characters or insert characters.
There is a reverse()
method in class StringBuffer
that reverses the order of the characters.
Let us conveniently forget about that method and re-write
our previous example
program:
public class ReverseTester { public static String reverse( String data ) { StringBuffer temp = new StringBuffer(); for ( int j=data.length()-1; j >= 0; j-- ) temp.append( data.charAt(j) ); return temp.toString(); } public static void main ( String[] args ) { System.out.println( reverse( "Hello" ) ); } }
In this version of reverse()
, only two new objects
are created: the StringBuffer
and the
String
object that is returned to the caller.